<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        html,
        body {
            height: 100%;
        }

        html {
            font-size: 100px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .form-container {
            height: 100%;
            overflow: auto;
            padding: 16px 0.16rem;
            font-size: 0.14rem;
            color: #666666;
        }

        .bottom-16 {
            margin-bottom: 4px;
        }

        .form-itm {
            display: flex;
            flex-direction: column;
        }

        .itm-input {
            height: 40px;
            outline: none;
            border: 1px solid #E5E5E5;
            padding-left: 0.05rem;
            border-radius: 5px;
        }

        .validate-code {
            display: flex;
        }

        .code-btn {
            flex: 1;
            margin-left: 0.16rem;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background-color: #F03D3C;
            color: #fff;
            outline: none;
            border: none;
            cursor: pointer;
            border-radius: 5px;
        }

        .code-input {
            flex: 1;
        }

        select {
            color: #666666;
        }

        .sub-btn {
            margin-top: 8px;
            width: 100%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background-color: #F03D3C;
            color: #fff;
            border-radius: 5px;
            outline: none;
            border: none;
            padding: 0 0.16rem;
        }
    </style>
</head>
<body>
<div class="form-container">
    <form id="testForm" class="form-content">
        <label class="form-itm">
            <span class="itm-title">收货人姓名</span>
            <input placeholder="收货人姓名" class="itm-input" type="text" name="acceptName">
        </label>
        <label class="form-itm">
            <span class="itm-title">手机号码</span>
            <input placeholder="请输入您的手机号码" class="itm-input" type="text" name="tel">
        </label>
        <label class="form-itm">
            <span class="itm-title">手机验证码</span>
            <div class="validate-code">
                <input placeholder="手机验证码" class="itm-input code-input" type="text" name="code">
                <button class="code-btn" type="button">获取验证码</button>
            </div>
        </label>
        <label class="form-itm">
            <span class="itm-title">选择地区</span>
            <select class="itm-input bottom-16" name="province">
                <option value="">请选择省份</option>
                <option value="1">湖南</option>
                <option value="2">广东</option>
            </select>
        </label>
        <label class="form-itm">
            <select class="itm-input" name="city">
                <option value="">请选择城市</option>
                <option value="1">长沙</option>
                <option value="2">益阳</option>
                <option value="3">深圳</option>
                <option value="4">广州</option>
                <option value="5">东莞</option>
            </select>
        </label>
        <label class="form-itm">
            <span class="itm-title">详细地址</span>
            <input placeholder="例如xx街道xx号" class="itm-input" type="text" name="detailArea">
        </label>
        <button type="submit" class="sub-btn">提交订单</button>
    </form>
    <!-- <button form="testForm" type="submit" class="sub-btn">提交订单</button> -->
</div>
</body>
<script>
  /* jsonData */
  Object.defineProperty(HTMLFormElement.prototype, 'jsonData', {
    get() {
      const jsondata = {};
      const formdata = new FormData(this);
      formdata.forEach((value, key) => {
        if (!jsondata[key]) {
          const _all = formdata.getAll(key);
          jsondata[key] = _all.length > 1 ? _all : formdata.get(key);
        }
      });
      return jsondata;
    },
  });
  /* formData */
  Object.defineProperty(HTMLFormElement.prototype, 'formData', {
    get() {
      return new FormData(this);
    },
  });
  /* urlData */
  Object.defineProperty(HTMLFormElement.prototype, 'urlData', {
    get() {
      const formdata = new FormData(this);
      const result = [];
      formdata.forEach((value, key) => {
        const _all = formdata.getAll(key);
        const _str = `${key}=${_all.join ? _all.join(',') : _all}`;
        result.push(_str);
      });
      return result.join('&');
    },
  });

  const form = document.querySelector('.form-content');
  form.addEventListener('submit', (evt) => {
    /** 阻止表单默认提交事件 */
    evt.preventDefault();
    /** 得到类似于 { xx: 'yyyy', ... } 的结果 */
    console.log(evt.target.jsonData);
    /** 得到 FormData 实例, 其中包含各种属性 */
    console.log(evt.target.formData);
    /** 得到类似于 xx=yyyy&aa=bbbb&... 的结果 */
    console.log(evt.target.urlData);
  });
</script>
</html>
